package com.synergy.stockflow.warehouse.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.warehouse.entity.Warehouse;
import com.synergy.stockflow.warehouse.service.WarehouseService;

import io.swagger.v3.oas.annotations.Operation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.type.SimpleTypeRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.ReactiveSetCommands;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;

    @GetMapping("/tree")
    public Result<List<Warehouse>> findAll() {
        List<Warehouse> warehouseList = warehouseService.list();
        return Result.success(warehouseList, "");
    }

    //分页查询
    @GetMapping
    public Map<String, Object> pag(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "warehouseName", required = false) String warehouseName,
            @RequestParam(name = "warehouseType", required = false) String warehouseType
    ) {
        Map<String, Object> ressult = new HashMap<>();
        try {
            QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
            if (warehouseName != null && !warehouseName.isEmpty()) {
                queryWrapper.lambda().like(Warehouse::getWarehouseName, warehouseName);
            }
            if (warehouseType != null && !warehouseType.isEmpty()) {
                queryWrapper.lambda().like(Warehouse::getWarehouseType, warehouseType);
            }
            Page<Warehouse> page = new Page<>(current, size);
            warehouseService.page(page, queryWrapper);
            ressult.put("success", true);
            ressult.put("total", page.getTotal());
            ressult.put("pages", page.getPages());
            ressult.put("date", page.getRecords());
        } catch (Exception e) {
            ressult.put("success", false);
            ressult.put("message", "分页查询失败:" + e.getMessage());
        }
        return ressult;
    }

    @PostMapping("/insert")
    public Map<String, Object> insert(@RequestBody Warehouse warehouse) {
        Map<String, Object> result = new HashMap<>();
        try {
            String nextWarehouse = warehouseService.generateNextWarehouseId();
            warehouse.setWarehouseId(nextWarehouse);

            Date now = new Date();
            warehouse.setCreateTime(now);
            warehouse.setUpdateTime(now);

            boolean success = warehouseService.save(warehouse);
            result.put("success", success);
            result.put("message", success ? "新增成功" : "新增失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增异常" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody Warehouse warehouse) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (warehouse.getWarehouseId() == null || warehouse.getWarehouseId().isEmpty()) {
                result.put("success", false);
                result.put("massage", "仓库Id不能为空");
            }
            warehouse.setUpdateTime(new Date());
            boolean success = warehouseService.updateById(warehouse);
            result.put("success", success);
            result.put("message", success ? "修改成功" : "修改失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "修改异常" + e.getMessage());
        }
        return result;
    }

    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = warehouseService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常" + e.getMessage());
        }
        return result;
    }
}

